package com.example.wenliangjiudian.service.impl;

import com.example.wenliangjiudian.entity.Customer;
import com.example.wenliangjiudian.entity.Room;
import com.example.wenliangjiudian.mapper.InLogMapper;
import com.example.wenliangjiudian.mapper.RoomMapper;
import com.example.wenliangjiudian.pojo.RoomDO;
import com.example.wenliangjiudian.service.CustomerService;
import com.example.wenliangjiudian.service.RoomService;
import com.example.wenliangjiudian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private InLogMapper inLogMapper;

    public List<Room> selectAll(){
        List<Room> roomList = roomMapper.selectAll();
        return roomList;
    }

    public List<RoomDO> listRoom(){
        List<RoomDO> roomList = roomMapper.listRoom();
        return roomList;
    }

    @Override
    public boolean inRoom(String roomNum, Customer customer) {
        // 先判断这个房间是否是未入住状态
        Room roomByRoomNum = roomMapper.getRoomByRoomNum(roomNum);
        if (roomByRoomNum == null) {
            return false;
        }
        if (roomByRoomNum.getStatus() != 1) {
            if (roomByRoomNum.getStatus() != 2) {
                return false;
            }
            /*if (customer.getIdcard() != "之前预定的人") {
                return false;
            }*/
        }
        //

        // 保存用户
        customerService.save(customer);

        // 修改房间状态
        Integer status = 3;//已入住
        roomMapper.updateStatus(roomNum, status);

        // 添加入住流水表
        Customer aaa = customerService.getByIdcard(customer.getIdcard());

        inLogMapper.insert(aaa.getId(), roomByRoomNum.getId());

        return true;
    }

    @Override
    public void updateStatus(String roomNum, Integer status) {
        roomMapper.updateStatus(roomNum, status);
    }

    @Override
    public void deleteRoom(String roomNum) {
        roomMapper.deleteRoom(roomNum);
    }

    @Override
    public boolean addRoom(String roomNum, Integer roomType, Integer price) {
        roomMapper.addRoom(roomNum,roomType,price);
        return true;
    }

    @Override
    public boolean updateRoom(String roomNum, Integer roomType, Integer price) {
        roomMapper.updateRoom(roomNum,roomType,price);
        return true;
    }

    @Override
    public Room getRoomByRoomNum(String roomNum) {
        Room room = roomMapper.getRoomByRoomNum(roomNum);
        return room;
    }
}
